/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mylhyl.circledialog.sample;

import com.mylhyl.circledialog.BaseCircleDialog;
import com.mylhyl.circledialog.ConstandCodes;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.BaseDialog;
import ohos.agp.window.service.WindowManager;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import java.util.Timer;
import java.util.TimerTask;

import static ohos.agp.animation.Animator.INFINITE;

/**
 * FgtAbility
 *
 * @since 2021-07-21
 */
public class FgtAbility extends Ability {
    private static final int DELAY = 2000;
    private static final int WIDTH = 150;
    private static final int DURATION = 800;
    private static final int ROTATE = 360;
    private static final int DELAY_TIME = 50;

    private BaseDialog dialog;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        WindowManager.getInstance().getTopWindow().get()
                .setStatusBarColor(Color.getIntColor("#3F51B5"));
        super.setUIContent(ResourceTable.Layout_ability_fragment);
        initViews();
    }

    private void initViews() {
        Button btnTest = (Button) findComponentById(ResourceTable.Id_btnTest);
        Text textResponse = (Text) findComponentById(ResourceTable.Id_textResponse);
        btnTest.setClickedListener(component -> {
            showLoadingDialog();
            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    runUi(() -> {
                        dialog.hide();
                        textResponse.setText(getString(ResourceTable.String_response));
                    });
                }
            }, DELAY);
        });
    }

    private void showLoadingDialog() {
        int size = PxUtils.vp2px(getContext(), WIDTH);
        dialog = BaseCircleDialog.init(getContext())
                .setLayoutId(ResourceTable.Layout_layout_dialog1, ConstandCodes.COMMONTYPE)
                .setViewConvertListener((holder, di) -> {
                    EventHandler eventHandler = new EventHandler(EventRunner.getMainEventRunner());
                    eventHandler.postTask(new Runnable() {
                        @Override
                        public void run() {
                            holder.getView(ResourceTable.Id_round_progress_bar).createAnimatorProperty()
                                    .rotate(ROTATE).setLoopedCount(INFINITE)
                                    .setDuration(DURATION).start();
                        }
                    }, DELAY_TIME);
                })
                .setCanceledOnTouchOutside(true)
                .setSize(size, size)
                .setTransparent(true);
        dialog.show();
    }

    /**
     * 主线程执行任务
     *
     * @param runnable
     */
    public static void runUi(Runnable runnable) {
        EventHandler eventHandler = new EventHandler(EventRunner.getMainEventRunner());
        eventHandler.postSyncTask(runnable);
    }
}
